package com.restfinder.user.service;

import java.io.Serializable;
import java.util.Date;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.restfinder.common.component.utils.PropertiesUtils;
import com.restfinder.common.component.utils.SecurityUtils;
import com.restfinder.user.component.PasswordUtils;
import com.restfinder.user.dao.DatabaseRoleRepository;
import com.restfinder.user.dao.RoleRepository;
import com.restfinder.user.dao.DatabaseUserRepository;
import com.restfinder.user.dao.UserRepository;
import com.restfinder.user.model.Role;
import com.restfinder.user.model.User;

@Service
@Transactional(rollbackFor=Exception.class)
public class UserService implements Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
	private UserRepository userRepository;
	@Autowired
	private RoleRepository roleRepository;
	@Autowired
	@Qualifier("authenticationManager")
	private AuthenticationManager authenticationManager;
	
	/**
	 * Saves the non-existing in the repository user.
	 * @param {@link User} new user
	 */
	public void register(User user) {
		user.setAvatarSrc(PropertiesUtils.getValue("user_avatar_src"));
		user.setEnabled(true);
		Role userRole = roleRepository.getByCode("ROLE_USER");
		user.add(userRole);
		user.setPassword(PasswordUtils.encodePassword(user.getPassword()));
		userRepository.save(user);
	}
	
	public User getLoggedUser() {
        String username = SecurityUtils.getAuthenticatedUsername();
        return getByUsername(username);
	}
	
	public User getByUsername(String login){
		return userRepository.getByUsername(login);
	}
	
	public boolean isUsernameUnique(String username, User user){
		return userRepository.isUsernameUnique(username, user);
	}
	
	public boolean authenticateUser(String login, String password) throws BadCredentialsException, DisabledException, UsernameNotFoundException {
		Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(login, password));
		if (authenticate.isAuthenticated()) {
			SecurityUtils.setAuthentication(authenticate);
			return true;
		} 
		return false;
	}

	public void update(User user) {
		this.userRepository.update(user);
	}
}
